/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.l2dot.gameserver.network.clientpackets;

import java.util.logging.Logger;

import com.l2dot.gameserver.datatables.SkillTable;
import com.l2dot.gameserver.model.L2Skill;
import com.l2dot.gameserver.model.actor.instance.L2PcInstance;
import com.l2dot.gameserver.network.serverpackets.ActionFailed;
import com.l2dot.util.Point3D;





/**
 * Fromat:(ch) dddddc
 * @author  -Wooden-
 */
public final class RequestExMagicSkillUseGround extends L2GameClientPacket
{
    private static final String _C__D0_2F_REQUESTEXMAGICSKILLUSEGROUND = "[C] D0:2F RequestExMagicSkillUseGround";
    private static Logger _log = Logger.getLogger(RequestExMagicSkillUseGround.class.getName());
    
    private int _x;
    private int _y;
    private int _z;
    private int _skillId;
    private boolean _ctrlPressed;
    private boolean _shiftPressed;
    
    @Override
    protected void readImpl()
    {
        _x = readD();
        _y = readD();
        _z = readD();
        _skillId = readD();
        _ctrlPressed = readD() != 0;
        _shiftPressed = readC() != 0;
    }
    
    /**
     * @see com.l2dot.gameserver.clientpackets.ClientBasePacket#runImpl()
     */
    @Override
    protected void runImpl()
    {
        // Get the current L2PcInstance of the player
        L2PcInstance activeChar = getClient().getActiveChar();
        
        if (activeChar == null)
            return;
        // Get the level of the used skill
        int level = activeChar.getSkillLevel(_skillId);
        if (level <= 0) 
        {
            activeChar.sendPacket(new ActionFailed());
            return;
        }
        
        // Get the L2Skill template corresponding to the skillID received from the client
        L2Skill skill = SkillTable.getInstance().getInfo(_skillId, level);
        
        // Check the validity of the skill
        if (skill != null)
        {
            activeChar.setCurrentSkillWorldPosition(new Point3D(_x , _y, _z));
            activeChar.useMagic(skill, _ctrlPressed, _shiftPressed);
        }
        else
        {
            activeChar.sendPacket(new ActionFailed());
            _log.warning("No skill found!!");
        }
    }
    
    /**
     * @see com.l2dot.gameserver.BasePacket#getType()
     */
    @Override
    public String getType()
    {
        return _C__D0_2F_REQUESTEXMAGICSKILLUSEGROUND;
    }
}